﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.BusinessLogicLayer;

namespace DeviceInfoManage
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!Object.Equals(Request.Cookies["adminUserName"], null))
                {
                    /*读取保存用户名的cookie信息,此信息保存在客户端,方便管理员减少信息输入*/
                    HttpCookie adminUserNameCookie = Request.Cookies["adminUserName"];
                    this.adminUserName.Text = adminUserNameCookie.Value;
                }
                adminUserName.Focus();
                /*当验证码输入完成后按回车键提交表单*/
                this.Validator.Attributes.Add("onkeypress", "if(event.keyCode==13){document.all.Btn_Login.click();return false;}");
            }
        }

        /*使用验证码可以防止网页被暴力破解*/
        protected void ChangeCode_Click(object sender, ImageClickEventArgs e)
        {
            /*重新载入验证码的图片*/
            this.ChangeCode.ImageUrl = "Validate.aspx";
        }

        protected void Btn_Login_Click(object sender, EventArgs e)
        {
            /*首先进行验证码的比对,正确的验证码在Validate.aspx的代码文件中使用Session变量已经保存*/
            //if (!string.IsNullOrEmpty(this.Validator.Text.Trim()) && Session["Code"] != null)
            //{
            //    if (String.Compare(Session["Code"].ToString(), this.Validator.Text, true) != 0)
            //    {
            //        Response.Write("<script>alert('对不起,你输入的验证码不正确!');</script>");
            //        return; /*返回，不执行此后的代码*/
            //    }
            //}
            //else
            //{
            //    Response.Write("<script>alert('请输入验证码!');</script>");
            //    return;
            //}
            /*密码经过md5哈希加密后再传给业务层进行和数据库中的登陆信息比对*/
            Admin admin = new Admin();
            admin.AdminUserName = this.adminUserName.Text.Trim();
            admin.AdminPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.adminPassword.Text.Trim(), "MD5").ToString();
            if (admin.checkAdmin()) /*如果密码比对成功*/
            {
                /*将管理员帐号保存到cookie变量和session变量中然后转到操作首页main.aspx*/
                CreateCookie();
                Session["adminUserName"] = admin.AdminUserName;
                Session["adminType"] = admin.AdminType;
                Session["UserName"] = admin.UserName;
                Session.Timeout = 10;
                //Response.Redirect("main.aspx");
                Response.Write("<script>top.location.href='main.aspx';</script>");
            }
            else /*否则错误信息保存在admin对象的ErrMessage属性中*/
            {
                Response.Write("<script>alert('" + admin.ErrMessage + "');</script>");
            }

        }
        protected void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.adminUserName.Text = "";
            this.adminPassword.Text = "";
            this.Validator.Text = "";
            this.ChangeCode.ImageUrl = "Validate.aspx";
        }

        /*如果帐号密码都正确，则调用本函数将帐号信息加入的客户端cookie中*/
        private void CreateCookie()
        {
            HttpCookie adminUserNameCookie = new HttpCookie("adminUserName");
            adminUserNameCookie.Value = this.adminUserName.Text;
            adminUserNameCookie.Expires = DateTime.MaxValue;
            Response.AppendCookie(adminUserNameCookie);
        }
    }

}
